import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('../components/Layout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: '',
          name: 'home',
          component: () => import('../views/layout/HomeView.vue'),
        },
        {
          path: 'about',
          name: 'about',
          component: () => import('../views/about/AboutView.vue'),
          meta: {
            title: '关于系统',
          },
        },
        {
          path: 'customers',
          name: 'customers',
          component: () => import('../views/customer/CustomerListView.vue'),
          meta: {
            title: '客户管理',
          },
        },
        {
          path: 'customers/create',
          name: 'customer-create',
          component: () => import('../views/customer/CustomerForm.vue'),
          meta: {
            title: '新增客户',
          },
        },
        {
          path: 'customers/:id/edit',
          name: 'customer-edit',
          component: () => import('../views/customer/CustomerForm.vue'),
          meta: {
            title: '编辑客户',
          },
        },
        {
          path: 'customers/:id',
          name: 'customer-detail',
          component: () => import('../views/customer/CustomerDetailView.vue'),
          meta: {
            title: '客户详情',
          },
        },
        {
          path: 'users',
          name: 'users',
          component: () => import('../views/user/UserView.vue'),
          meta: {
            title: '用户管理',
          },
        },
        {
          path: 'user-logs',
          name: 'user-logs',
          component: () => import('../views/user/UserLogView.vue'),
          meta: {
            title: '操作日志',
          },
        },
        {
          path: 'departments',
          name: 'departments',
          component: () => import('../views/department/DepartmentView.vue'),
          meta: {
            title: '部门管理',
          },
        },
        {
          path: 'roles',
          name: 'roles',
          component: () => import('../views/permission/RoleView.vue'),
          meta: {
            title: '角色管理',
          },
        },
        {
          path: 'permissions',
          name: 'permissions',
          component: () => import('../views/permission/PermissionView.vue'),
          meta: {
            title: '权限管理',
          },
        },
        {
          path: 'industries',
          name: 'industries',
          component: () => import('../views/industry/IndustryView.vue'),
          meta: {
            title: '行业管理',
          },
        },
        {
          path: 'visits',
          name: 'visits',
          component: () => import('../views/visit/VisitListView.vue'),
          meta: {
            title: '拜访管理',
          },
        },
        {
          path: 'visits/create',
          name: 'visit-create',
          component: () => import('../views/visit/VisitForm.vue'),
          meta: {
            title: '新增拜访',
          },
        },
        {
          path: 'visits/:id/edit',
          name: 'visit-edit',
          component: () => import('../views/visit/VisitForm.vue'),
          meta: {
            title: '编辑拜访',
          },
        },
        {
          path: 'visits/:id',
          name: 'visit-detail',
          component: () => import('../views/visit/VisitDetailView.vue'),
          meta: {
            title: '拜访详情',
          },
        },
        {
          path: 'clues',
          name: 'clues',
          component: () => import('../views/clue/ClueListView.vue'),
          meta: {
            title: '线索管理',
          },
        },
        {
          path: 'clues/create',
          name: 'clue-create',
          component: () => import('../views/clue/ClueForm.vue'),
          meta: {
            title: '新增线索',
          },
        },
        {
          path: 'clues/:id/edit',
          name: 'clue-edit',
          component: () => import('../views/clue/ClueForm.vue'),
          meta: {
            title: '编辑线索',
          },
        },
        {
          path: 'clues/:id',
          name: 'clue-detail',
          component: () => import('../views/clue/ClueDetailView.vue'),
          meta: {
            title: '线索详情',
          },
        },
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/auth/LoginView.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/auth/RegisterView.vue'),
    },
  ],
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else if ((to.path === '/login' || to.path === '/register') && token) {
    next('/')
  } else {
    next()
  }
})

export default router
